package com.example.service.impl;

import com.example.mapper.EmpMapper;
import com.example.mapper.ProcessMapper;
import com.example.pojo.*;
import com.example.mapper.PositionTransferMapper;
import com.example.pojo.Process;
import com.example.service.IPositionTransferService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.vo.ResultVo;
import com.example.vo.per.EmpTurnover;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 岗位调动申请表 服务实现类
 * </p>
 *
 * @author wsl
 * @since 2022-05-07
 */
@Service
public class PositionTransferServiceImpl extends ServiceImpl<PositionTransferMapper, PositionTransfer> implements IPositionTransferService {

    @Autowired
    private PositionTransferMapper ptm;
    @Autowired
    private ProcessMapper prm;
    @Autowired
    private EmpMapper em;

    @Override
    public PageInfo<EmpTurnover> selResByEmpIdList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<EmpTurnover> list = em.selectB();
        PageInfo<EmpTurnover> empTurnoverPageInfo = new PageInfo<>(list);
        List<EmpTurnover> list1 = empTurnoverPageInfo.getList();
        for (EmpTurnover empTurnover : list1) {
            System.err.println(empTurnover.getEmpId() + " ---> ");
            PositionTransfer positionTransfer = ptm.selectByEmpId(empTurnover.getEmpId());
            empTurnover.setPositionTransfer(positionTransfer);
        }
        return empTurnoverPageInfo;
    }

    @Override
    @Transactional
    public ResultVo addPos(PositionTransfer pt) {
        // 新创流程
        Process process = new Process();
        process.setName("员工壮岗申请");
        process.setPrefix("YGZG");
        process.setClassifyId(1);
        prm.insert(process);

        // 新增岗位调岗记录
        pt.setProcessId(process.getId());
        pt.setOriginalDeptId(pt.getDeptId());
        pt.setCreateTime(LocalDateTime.now());
        ptm.insert(pt);

        return ResultVo.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void uptPos(Integer[] ids) {
        for (Integer id : ids) {
            PositionTransfer byId = ptm.selectById(new PositionTransfer().setId(id));
            // 更新员工岗位记录
            Emp emp = new Emp();
            emp.setSuperiorId(byId.getSupeNumber());
            emp.setDeptId(byId.getTargetDeptId());
            emp.setPositionId(byId.getTargetPositionId());
            em.updateById(emp);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delPos(Integer[] ids) {
        for (Integer id : ids) {
            ptm.deleteById(id);
        }
    }

}
